<?php
/*
 * This class is used for as Model for Voucher reference table 
 */

class Application_Model_VoucherRef extends Application_Model_Base {

    protected $_name = 'voucher_ref';
    protected $_primaryKey = 'id';
    
    
    

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('vref' => 'voucher_ref'))
                ->where('id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get VoucherRefs
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchVoucherRefs($where = "", $limit = "", $offset = "0", $order = 'id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('vref' => 'voucher_ref'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        
        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getVoucherRefCount($where) {
        $select = $this->select()
                ->from(array('vref' => 'voucher_ref'), Array('cnt' => 'count(*)'));
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete VoucherRefs
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteVoucherRef($vreftArray) {
        $loginModel = new Model_Login();
        if (is_array($vreftArray)) {
            foreach ($vreftArray as $_VoucherRef) {
                $VoucherRefData = Array();
                $VoucherRefData = $this->fetchEntryById($_VoucherRef);
                if (count($VoucherRefData) > 0) {
                    $this->delete('id = ' . (int) $_VoucherRef);
                }
            }
        } else {
            $id = $vreftArray;
            $VoucherRefData = $this->fetchEntryById($id);
            if (count($VoucherRefData) > 0) {
                $this->delete('id = ' . (int) $id);
            }
        }
    }

}
